//
//  CustomSegmentedControl.m
//  玻璃易购
//
//  Created by 孔焕宁 on 16/4/21.
//  Copyright © 2016年 艾磊. All rights reserved.
//

#import "CustomSegmentedControl.h"

typedef void(^OnClickIndex)(NSInteger);
@implementation CustomSegmentedControl
{
    CGFloat titleSize;
    CGSize size;
    UIView *selectView;
    UIView *backView;
    UIButton *tempButton;
    NSMutableArray *buttos;
    OnClickIndex onClick_Index;
}


- (instancetype)initWithItems:(NSArray *)items onClickIndex:(void (^)(NSInteger index))onClickIndex
{
    self = [super init];
    if(self)
    {
        onClick_Index = onClickIndex;
        [self setUI:items
               size:[self setStringRect:items font:15.0f]
              color:[UIColor whiteColor]
        selectColor:[UIColor whiteColor]];
    }
    return self;
}










/**
 *  设置UI
 *
 *  @param items       字符串数组
 *  @param size        单字符串大小
 *  @param color       未选中颜色
 *  @param selectColor 选中颜色
 */
- (void)setUI:(NSArray *)items size:(CGSize)cutomSize color:(UIColor *)color selectColor:(UIColor *)selectColor
{
    self.color = color;
    self.selectedColor = selectColor;
    size = cutomSize;
    self.titleColor = [UIColor whiteColor];
    self.titleSelectColor = [UIColor colorWithRed:52/255.0 green:152/255.0 blue:219/255.0 alpha:1.0];
    
    backView    = [UIView new];
    selectView  = [UIView new];
    buttos      = [NSMutableArray new];
    
    [self addSubview:backView];
    [self addSubview:selectView];
    
    self.frame = backView.frame = CGRectMake(0, 0, size.width*items.count, size.height);
    self.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height);
    self.showsHorizontalScrollIndicator = self.showsVerticalScrollIndicator = NO;
    selectView.frame = CGRectMake(0, 0, size.width, size.height);
    
    backView.backgroundColor = color;
    selectView.backgroundColor = selectColor;
    
    self.center = CGPointMake([[UIScreen mainScreen] bounds].size.width/2, self.center.y);
    
    for(int i=0 ; i<items.count ; i++)
    {
        NSString *str = items[i];;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundColor:[UIColor clearColor]];
        button.frame = CGRectMake(i*size.width, 0, size.width, size.height);
        [button setTitle:str forState:UIControlStateNormal];
        [button setTitleColor:self.titleColor forState:UIControlStateNormal];
        button.tag = 1100+i;
        button.titleLabel.font = [UIFont systemFontOfSize:titleSize];
        [button addTarget:self action:@selector(onClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        [buttos addObject:button];
        if( i==0 )
        {
            [button setTitleColor:self.titleSelectColor forState:UIControlStateNormal];
            tempButton = button;
        }
    }
}







- (void)onClickAction:(UIButton *)button
{
    if(tempButton.tag == button.tag)
    {
        return;
    }
    
    NSInteger index = button.tag - 1100;
    CGPoint center = button.center;
    if(center.x != selectView.center.x)
    {
        
        [button setTitleColor:self.titleSelectColor forState:UIControlStateNormal];
        CGRect selectRect = selectView.frame;
        CGRect buttonRect = button.frame;
        buttonRect.origin.y = selectRect.origin.y;
        buttonRect.size.height = selectRect.size.height;
        [UIView animateWithDuration:0.3 animations:^{
            selectView.frame = buttonRect;
        }completion:^(BOOL finished) {
            [tempButton setTitleColor:self.titleColor forState:UIControlStateNormal];
            tempButton = button;
        }];
    }
    onClick_Index(index);
}










- (void)setSelectViewHeight
{
    if(self.times>0 && self.times <1)
    {
        selectView.frame = CGRectMake(selectView.frame.origin.x , size.height*  (1-self.times), size.width, size.height*self.times);
    }
}



/**
 *  修改字体颜色
 */
- (void)setTitleColor
{
    for(int i=0;i<buttos.count;i++)
    {
        UIButton *button = buttos[i];
        [button setTitleColor:self.titleColor forState:UIControlStateNormal];
        if(i == 0)
        {
            [button setTitleColor:self.titleSelectColor forState:UIControlStateNormal];
        }
        
    }
}


/**
 *  设置这个控件的颜色值
 *
 */
- (void)setBackGroundColor
{
    backView.backgroundColor = self.color;
    selectView.backgroundColor = self.selectedColor;
}





/**
 *  设置选中view的位置
 */
- (void)setSelectViewIndex
{
    selectView.frame = CGRectMake(size.width*self.selectIndex, 0, size.width, size.height);
}





/**
 *  循环判断items里面字符最长字符的宽高大小
 *
 *  @param items 所有字符串
 *  @param index 字符的大小
 *  @return 返回一个宽度用以作为所有按钮的宽度
 */
- (CGSize)setStringRect:(NSArray *)items font:(NSInteger)index
{
    titleSize = index;
    CGFloat width = 0.0;
    CGFloat height = 0.0;
    for(NSString *str in items)
    {
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:index]};
        CGSize customSize = [str boundingRectWithSize:CGSizeMake(320, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
        if(customSize.width+20>width)
        {
            width = ScreenW/items.count;
        }
        if(customSize.height+10>height)
        {
            height = customSize.height+10;
        }
    }
    return CGSizeMake(width, height);
}




















- (void)setColor:(UIColor *)color
{
    _color = color;
    [self setBackGroundColor];
}

- (void)setSelectedColor:(UIColor *)selectedColor
{
    _selectedColor = selectedColor;
    [self setBackGroundColor];
}

- (void)setTitleColor:(UIColor *)titleColor
{
    _titleColor = titleColor;
    [self setTitleColor];
}

- (void)setTitleSelectColor:(UIColor *)titleSelectColor
{
    _titleSelectColor = titleSelectColor;
    [self setTitleColor];
}

- (void)setTimes:(CGFloat)times
{
    _times = times;
    [self setSelectViewHeight];
}

- (void)setSelectIndex:(NSInteger)selectIndex
{
    _selectIndex = selectIndex;
    UIButton *button = (UIButton *)[self viewWithTag:1100+selectIndex];
    [self onClickAction:button];
}
@end
